package urjc.as.emergencies.client.jpanels.missions;

import java.awt.Font;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.border.TitledBorder;

import net.miginfocom.swing.MigLayout;

import org.restlet.data.Form;
import org.restlet.data.Status;

import urjc.as.emergencies.client.CommToolkit;
import urjc.as.emergencies.client.TextArea;
import urjc.as.emergencies.data.PatientInfo;

@SuppressWarnings("serial")
public class PanelPatientInfo extends JPanel {
	private JTextField textFieldName;
	private JTextField textFieldEdad;
	private JTextField textFieldHistorial;
	private TextArea textArea;
	private PatientInfo infoPatient;
	private JTextPane textPaneDiagnosis;
	private JComboBox comboBoxSex;

	/**
	 * Create the panel.
	 */
	public PanelPatientInfo(TextArea _textArea, PatientInfo _infoPaInfo) {
		this.textArea = _textArea;
		this.infoPatient = _infoPaInfo;
		
		setBorder(new TitledBorder(null, "Informacion del Paciente", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		setLayout(new MigLayout("", "[grow][grow]", "[][][][grow][][]"));
		
		JLabel lblNombre = new JLabel("Nombre: ");
		lblNombre.setFont(new Font("Dialog", Font.BOLD, 12));
		add(lblNombre, "cell 0 0,alignx left");
		
		textFieldName = new JTextField();
		add(textFieldName, "flowx,cell 1 0,growx");
		textFieldName.setColumns(10);
		
		JLabel lblEdad = new JLabel("Edad:");
		lblEdad.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblEdad, "cell 1 0,alignx left");
		
		textFieldEdad = new JTextField();
		add(textFieldEdad, "cell 1 0,growx");
		textFieldEdad.setColumns(10);
		
		JLabel lblSexo = new JLabel("Sexo:");
		lblSexo.setFont(new Font("Tahoma", Font.BOLD, 11));
		add(lblSexo, "cell 1 0");
		
		comboBoxSex = new JComboBox();
		comboBoxSex.setModel(new DefaultComboBoxModel(new String[] {"Hombre", "Mujer"}));
		add(comboBoxSex, "cell 1 0,growx");
		
		JLabel lblDiagnstico = new JLabel("Diagnóstico");
		lblDiagnstico.setFont(new Font("Dialog", Font.BOLD, 12));
		add(lblDiagnstico, "cell 0 2");
		
		textPaneDiagnosis = new JTextPane();
		add(textPaneDiagnosis, "cell 0 3 2 1,grow");
		
		JLabel lblNewLabel = new JLabel("Historial");
		lblNewLabel.setFont(new Font("Dialog", Font.BOLD, 12));
		add(lblNewLabel, "cell 0 4,alignx left");
		
		textFieldHistorial = new JTextField();
		add(textFieldHistorial, "cell 1 4,growx");
		textFieldHistorial.setColumns(10);
		
		JButton ButtonCommit = new JButton("Actualizar Cambios");
		ButtonCommit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				updateInfoPatient();
			}
		});
		add(ButtonCommit, "cell 0 5 2 1,alignx center");

		completePanel();
	}

	protected void updateInfoPatient() {
		//Creo el formulario con las modificaciones
		Form form = new Form();
		
		form.add(PatientInfo.NAME, textFieldName.getText());
		form.add(PatientInfo.AGE, textFieldEdad.getText());
		form.add(PatientInfo.DIAGNOSIS, textPaneDiagnosis.getText());
		form.add(PatientInfo.SEX, comboBoxSex.getSelectedItem().toString());
	
		// Hacemos la petición para crear un nuevo mailbox de
		// usuario, sobre la URL adecuada
		Status state = CommToolkit.sendPatch(infoPatient.getUri(), form);
		
		// Se muestra el estado
		textArea.append(state.getDescription());
		
	}

	private void completePanel() {
		this.textFieldName.setText(infoPatient.getName());
		this.textFieldEdad.setText(infoPatient.getEdad()+"");
		this.textFieldHistorial.setText(infoPatient.getHistorial());
		this.textPaneDiagnosis.setText(infoPatient.getDiagnostico());
		
		if (infoPatient.getName()!= null) {
			textFieldName.setEditable(false);
		}
	}

	
}
